package com.tbit.uqbike.client.util;

import cn.hutool.extra.servlet.ServletUtil;
import com.tbit.uqbike.client.constant.enums.SourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.nio.charset.StandardCharsets;

/**
 * 请求来源工具
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/9/3
 */
@Slf4j
public class SourceUtil {
    /**
     * 获取当前请求来源
     */
    public static SourceType getCurrentSource() {
        String header = ServletUtil.getHeader(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(), "ordersource", StandardCharsets.UTF_8);
        try {
            return SourceType.getByCode(Integer.parseInt(header));
        } catch (Exception e) {
            log.error("获取请求来源失败：{}", e.getMessage());
            return SourceType.UNKNOWN;
        }
    }
}
